package net.love5z.web.dao;

import java.util.List;

import net.love5z.web.dao.dto.ShortUrlDto;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

public interface ShortUrlMapper {

	@Select({ "select", "id, opCode, longUrl, accessTimes, opNum",
			"from ShortUrl order by opNum desc", "limit 0, 10" })
	@Results({
			@Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "opCode", property = "opCode", jdbcType = JdbcType.VARCHAR),
			@Result(column = "longUrl", property = "longUrl", jdbcType = JdbcType.VARCHAR),
			@Result(column = "accessTimes", property = "accessTimes", jdbcType = JdbcType.BIGINT),
			@Result(column = "opNum", property = "opNum", jdbcType = JdbcType.INTEGER) })
	List<ShortUrlDto> list();

	@Select({ "select", "longUrl", "from ShortUrl", "where opCode = #{opCode}" })
	String getLongUrlByOpCode(@Param("opCode") String opCode);

	@Select({ "select", "id, opCode, longUrl, accessTimes, opNum",
			"from ShortUrl", "where longUrl = #{longUrl}", "limit 0, 1" })
	@Results({
			@Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "opCode", property = "opCode", jdbcType = JdbcType.VARCHAR),
			@Result(column = "longUrl", property = "longUrl", jdbcType = JdbcType.VARCHAR),
			@Result(column = "accessTimes", property = "accessTimes", jdbcType = JdbcType.BIGINT),
			@Result(column = "opNum", property = "opNum", jdbcType = JdbcType.INTEGER) })
	ShortUrlDto getByLongUrl(@Param("longUrl") String longUrl);

	@Select({ "select", "max(opNum)", "from ShortUrl" })
	int getMaxOpNum();

	@Insert({
			"insert into ShortUrl (opCode, longUrl, accessTimes, opNum)",
			"values (#{opCode,jdbcType=VARCHAR}, #{longUrl,jdbcType=VARCHAR}, #{accessTimes,jdbcType=BIGINT}, #{opNum,jdbcType=INTEGER})" })
	@SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
	int insert(ShortUrlDto newSu);

	@Select({ "select", "id, opCode, longUrl, accessTimes, opNum",
			"from ShortUrl", "where opCode = #{opCode}", "limit 0, 1" })
	@Results({
			@Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "opCode", property = "opCode", jdbcType = JdbcType.VARCHAR),
			@Result(column = "longUrl", property = "longUrl", jdbcType = JdbcType.VARCHAR),
			@Result(column = "accessTimes", property = "accessTimes", jdbcType = JdbcType.BIGINT),
			@Result(column = "opNum", property = "opNum", jdbcType = JdbcType.INTEGER) })
	ShortUrlDto getByOpCode(@Param("opCode") String opCode);

	@Update({ "update ShortUrl", "set accessTimes = accessTimes+1",
			"where id = #{id}" })
	int upDataAccessTimesById(@Param("id") long id);

	@Select({ "select", "count(id)", "from ShortUrl" })
	int count();

	@Select({ "select", "id, opCode, longUrl, accessTimes, opNum",
			"from ShortUrl order by opNum desc", "limit #{pageStart}, #{pageCount}" })
	@Results({
			@Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "opCode", property = "opCode", jdbcType = JdbcType.VARCHAR),
			@Result(column = "longUrl", property = "longUrl", jdbcType = JdbcType.VARCHAR),
			@Result(column = "accessTimes", property = "accessTimes", jdbcType = JdbcType.BIGINT),
			@Result(column = "opNum", property = "opNum", jdbcType = JdbcType.INTEGER) })
	List<ShortUrlDto> listPage(@Param("pageStart")int pageStart, @Param("pageCount")int pageCount);

}
